package com.qfedu.empproject.dao.impl;

import com.qfedu.empproject.dao.ManagerDao;
import com.qfedu.empproject.entity.Emp;
import com.qfedu.empproject.entity.Manager;
import com.qfedu.empproject.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;


public class ManagerDaoImpl implements ManagerDao {
    private QueryRunner queryRunner = new QueryRunner();
    @Override
    public int insert(Manager manager) {
        int insert = 0;
        try {
            DbUtils.begin();
            String sql = "insert into manager(name,password) values(?,?)";
            insert = queryRunner.update(DbUtils.getConnection(),
                    sql, manager.getName(), manager.getPassword());
            DbUtils.commit();
        } catch (SQLException e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return insert;
    }

    @Override
    public Manager selectOne(String username) {
        Manager manager = null;
        try {
            DbUtils.begin();
            String sql = "select * from manager where name=?";
            manager = queryRunner.query(DbUtils.getConnection(), sql,
                    new BeanHandler<>(Manager.class), username);
            DbUtils.commit();
        } catch (SQLException e) {
            DbUtils.rollback();
            e.printStackTrace();
        }
        return manager;
    }
}
